package com.pearlsoft.webapp.core.web.session;

import java.io.Serializable;
import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

/**
 * 
 * @Creator haitingr
 * @Create-Date 2010-12-28
 */
public class SessionProfile implements Principal, Serializable, HttpSessionBindingListener {

	private static final long serialVersionUID = 345690851124471083L;
	
	/**
	 * COOKIE 名称
	 */
	public static final String COOKIE_USERNAME = "PEARLSOFT_USERNAME";
	
	public static final String COOKIE_USERCODE = "PEARLSOFT_USERCODE";
	public static final String COOKIE_USERID = "PEARLSOFT_USERID";
	
	public static final String COOKIE_ORGID ="PEARLSOFT_ORGID";
	
	
	public static final String COOKIE_STATE = "USER_STATE";
	public static final String USER_STATE_LOGIN="0";
	public static final String USER_STATE_LOCK="1";
	public static final String USER_STATE_UNLOCK="2";
	
	public static final String ADMIN_CODE= "admin";
	
	public static final String PROFILE_TAG = "_session_profile";

	private String userCode = "";
	
	private String userId="";
	
	private String userName="";
	
	private Map<String,Map<String, Object>> menuMap = new HashMap<String,Map<String, Object>>();

	private long createTime = System.currentTimeMillis();

	public void valueBound(HttpSessionBindingEvent evt) {
		SessionTracker.login(evt.getSession());
	}

	public void valueUnbound(HttpSessionBindingEvent evt) {
		SessionTracker.logout(evt.getSession());
	}


	public SessionProfile(String userCode){
		this.userCode=userCode;
	}

	public long getCreateTime() {
		return this.createTime;
	}

	public String getUserCode() {
		return userCode;
	}

	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (createTime ^ (createTime >>> 32));
		result = prime * result
				+ ((userCode == null) ? 0 : userCode.hashCode());
		result = prime * result + ((userId == null) ? 0 : userId.hashCode());
		result = prime * result
				+ ((userName == null) ? 0 : userName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SessionProfile other = (SessionProfile) obj;
		if (createTime != other.createTime)
			return false;
		if (userCode == null) {
			if (other.userCode != null)
				return false;
		} else if (!userCode.equals(other.userCode))
			return false;
		if (userId == null) {
			if (other.userId != null)
				return false;
		} else if (!userId.equals(other.userId))
			return false;
		if (userName == null) {
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "SessionProfile [createTime=" + createTime + ", userCode="
				+ userCode + ", userId=" + userId + ", userName=" + userName
				+ "]";
	}

	public String getName() {
		return this.userCode;
	}

	public Map<String, Map<String, Object>> getMenuMap() {
		return menuMap;
	}

	public void setMenuMap(Map<String, Map<String, Object>> menuMap) {
		this.menuMap = menuMap;
	}

}
